package com.blacksheep.mtm.controller;


import com.blacksheep.mtm.common.utils.ResultUtil;
import com.blacksheep.mtm.dto.report.OrderCount;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.common.Result;
import com.blacksheep.mtm.pojo.manager.TbOrder;
import com.blacksheep.mtm.service.ReportService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api("报表查询")
public class ReportController {

    @Autowired
    private ReportService reportService;
    @RequestMapping(value = "/report/order/count",method = RequestMethod.GET)
    @ApiOperation(value = "获取订单数量")
    public Result<OrderCount> getOrderList(){
        OrderCount result = reportService.getOrderCount();
        return  new ResultUtil<OrderCount>().setData(result);
    }


    @RequestMapping(value = "/report/delivery/perform",method = RequestMethod.GET)
    @ApiOperation(value = "配送员配送报表")
    public DataTablesResult getDeliveryPerform(int draw, int start, int length, String searchKey,
                                         String minDate, String maxDate,
                                         @RequestParam("search[value]") String search,
                                         @RequestParam("order[0][column]") int orderCol,
                                         @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"name","order_nums", "cost","sales", "rate"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        DataTablesResult result= reportService.getDeliveryPerformanceReport(draw,start,length,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }


    @RequestMapping(value = "/report/delivery/perform/total",method = RequestMethod.GET)
    @ApiOperation(value = "配送员报表统计")
    public Result<Integer> getDeliveryPerformTotal(){
        int result = reportService.getDeliveryPerformTotal();
        return  new ResultUtil<Integer>().setData(result);
    }



    @RequestMapping(value = "/report/box/use",method = RequestMethod.GET)
    @ApiOperation(value = "冰包使用")
    public DataTablesResult getBoxUserRecords(int draw, int start, int length, String searchKey,
                                         String minDate, String maxDate,
                                         @RequestParam("search[value]") String search,
                                         @RequestParam("order[0][column]") int orderCol,
                                         @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"b.box_code","d.codes", "b.create_time","prov", "o.order_id", "user", "r.create_time", "r.operator", "b.unbind_time"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        DataTablesResult result= reportService.getBoxUseRecords(draw,start,length,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }

    @RequestMapping(value = "/report/ice/history",method = RequestMethod.GET)
    @ApiOperation(value = "冰排使用")
    public DataTablesResult getIceHistory(int draw, int start, int length, String searchKey,
                                              String minDate, String maxDate,
                                              @RequestParam("search[value]") String search,
                                              @RequestParam("order[0][column]") int orderCol,
                                              @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"b.box_code","d.codes", "b.create_time","prov", "o.order_id", "user", "r.create_time", "r.operator", "b.unbind_time"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        DataTablesResult result= reportService.getIceUseRecords(draw,start,length,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }
    @RequestMapping(value = "/report/ice/now",method = RequestMethod.GET)
    @ApiOperation(value = "冰排现在状态")
    public DataTablesResult getIceNowStatus(int draw, int start, int length, String searchKey,
                                          @RequestParam("search[value]") String search,
                                          @RequestParam("order[0][column]") int orderCol,
                                          @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"d.code","d.type","prov", "i.create_time", "ou.create_time"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        DataTablesResult result= reportService.getIceStatusList(draw,start,length,searchKey,orderColumn,orderDir);
        return result;
    }


    @RequestMapping(value = "/report/order",method = RequestMethod.GET)
    @ApiOperation(value = "下单报表")
    public DataTablesResult getOrderReport(int draw, int start, int length, String searchKey,
                                           String minDate, String maxDate,
                                            @RequestParam("search[value]") String search,
                                            @RequestParam("order[0][column]") int orderCol,
                                            @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"m.realname","order_nums","item_nums", "sales_total"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "order_nums";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "asc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        DataTablesResult result= reportService.getOrderReport(draw,start,length,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }
}
